/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.ccut.titan.component.impl;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.ccut.titan.component.IActionContext;
import edu.ccut.titan.component.IParameterInfo;

/**
 * {@link IActionContext} implementation
 * 
 * @see IActionContext
 * @author LeslieGu
 * 
 */
public class TitanActionContext extends AbstractTitanContext implements
		IActionContext {

	private HttpServletRequest request;

	private HttpServletResponse response;

	private Map<String, Object> logicMap;

	public TitanActionContext(String id, HttpServletRequest request,
			HttpServletResponse response) {
		
		super(id);

		this.request = request;
		this.response = response;
		this.logicMap = new HashMap<String, Object>();
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	@Override
	public Object getValue(String key) {
		return this.getValue(key, IParameterInfo.SCOPE_LOGIC);
	}

	@Override
	public void setValue(String key, Object value) {
		this.setValue(key, value, IParameterInfo.SCOPE_LOGIC);
	}

	@Override
	public Object getValue(IParameterInfo parameterInfo) {
		if (parameterInfo != null) {
			return this.getValue(parameterInfo.getValueKey(), parameterInfo
					.getScope());
		}

		return null;
	}

	@Override
	public void setValue(IParameterInfo parameterInfo, Object value) {
		if (parameterInfo != null) {
			this.setValue(parameterInfo.getValueKey(), value, parameterInfo
					.getScope());
		}
	}

	private Object getValue(String key, int scope) {
		Object value = null;

		switch (scope) {
		case IParameterInfo.SCOPE_LOGIC: {
			value = this.logicMap.get(key);

			if (value != null) {
				return value;
			}
			// break
		}
		case IParameterInfo.SCOPE_REQUEST: {
			value = this.request.getParameter(key);
			if (value != null) {
				return value;
			}

			value = this.request.getAttribute(key);
			if (value != null) {
				return value;
			}
			// break;
		}
		case IParameterInfo.SCOPE_SESSION: {
			value = this.request.getSession().getAttribute(key);
			if (value != null) {
				return value;
			}
			// break;
		}
		case IParameterInfo.SCOPE_APPLICATOIN: {
			value = this.request.getSession().getServletContext().getAttribute(
					key);
			if (value != null) {
				return value;
			}
			// break;
		}
		default: {
			return value;
		}
		}
	}

	private void setValue(String key, Object value, int scope) {
		switch (scope) {
		case IParameterInfo.SCOPE_LOGIC: {
			this.logicMap.put(key, value);
			break;
		}
		case IParameterInfo.SCOPE_REQUEST: {
			this.request.setAttribute(key, value);
			break;
		}
		case IParameterInfo.SCOPE_SESSION: {
			this.request.getSession().setAttribute(key, value);
			break;
		}
		case IParameterInfo.SCOPE_APPLICATOIN: {
			this.request.getSession().getServletContext().setAttribute(key,
					value);
			break;
		}
		}
	}
}
